<?php
declare (strict_types = 1);

namespace app\api\controller\auth;

use app\api\controller\Api;
use app\common\system\model\AuthRole;
use util\Format;

class Role extends Api
{
    public function index()
    {
        $where = [];
        $post = $this->request->post();
        if (isset($post['id']) && $post['id'] > 0) $where[] = ['id', '=', $post['id']];
        if (isset($post['keyword']) && $post['keyword'] != '') $where[] = ['title', 'like', "%{$post['keyword']}%"];

        list($cfg) = Format::pagination($post);
        $model = new AuthRole();
        $pg = $model->where($where)->paginate($cfg);
        $listData = $pg->getCollection();

        $result['list'] = $listData;
        $result['pagination'] = Format::pgResult($pg);
        return success($result, '获取数据成功');
    }

    public function picker()
    {
        $model = new AuthRole();
        $list = $model->field('id, title')->select();
        return success($list, '获取数据成功');
    }

    // 读取角色详情
    public function read()
    {
        $post = $this->request->post();
        if (empty($post['id'])) return error([], '参数错误');

        $where = [];
        $where[] = ['id', '=', $post['id']];
        $model = new AuthRole();
        $row = $model->where($where)->find();

        if ($row) {
            $result = $row;
            return success($result, '获取数据成功');
        } else {
            return error([], '获取数据失败');
        }
    }

    // 创建角色
    public function create()
    {
        $post = $this->request->post();
        // 创建的时候清除干扰的信息
        unset($post['id']);
        $validate = new \app\common\system\validate\AuthRoleValidate();
        if (!$validate->scene('create')->check($post)) return error([], $validate->getError());

        $user = $this->request->user;
        $post['operator_id'] = $user['id'];
        $model = new \app\common\system\model\AuthRole();
        $saveRs = $model->save($post);

        if ($saveRs) {
            return success([], '保存成功');
        } else {
            return error([], '保存失败');
        }
    }

    // 编辑角色权限
    public function save()
    {
        $post = $this->request->post();

        $validate = new \app\common\system\validate\AuthRoleValidate();
        if (!$validate->scene('edit')->check($post)) return error([], $validate->getError());

        $where = [];
        $where[] = ['id', '=', $post['id']];
        $model = new \app\common\system\model\AuthRole();
        $row = $model->where($where)->find();

        $user = $this->request->user;
        $post['operator_id'] = $user['id'];
        $saveRs = $row->save($post);
        if ($saveRs) {
            return success([], '保存成功');
        } else {
            return error([], '保存失败');
        }
    }

    // 删除指定角色
    public function delete()
    {
        $post = $this->request->post();
        if (!isset($post['id'])) return error([], '参数错误');

        $where = [];
        $where[] = ['id', '=', $post['id']];
        $model = new \app\common\system\model\AuthRole();
        $row = $model->where($where)->find();
        if (empty($row)) return error([], '角色不存在');

        $rs = $row->delete();
        if ($rs) {
            return success([], '删除成功');
        } else {
            return error([], '删除失败');
        }
    }
}